/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeViewPanel.java
 *
 * Created on 27/11/2011, 20:34:41
 */
package proj5labbd.graphics.departamento;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

/*
 * Painel de visualização de departamentos
 */
public class DeViewPanel extends OperationPanel {

    DBInterface deptInterface;
    /** Creates new form DeViewPanel */
    public DeViewPanel(DeptFrame frame, DBInterface dbInterface) {
        super(frame,dbInterface);
        initComponents();
        jbtView.addActionListener(new DeptAction());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnParam = new javax.swing.JPanel();
        jlbNome = new javax.swing.JLabel();
        jftxtTel = new javax.swing.JFormattedTextField();
        jlbTel = new javax.swing.JLabel();
        jtxtNome = new javax.swing.JTextField();
        jlbEm = new javax.swing.JLabel();
        jlbNFunc = new javax.swing.JLabel();
        jtxtNFunc = new javax.swing.JTextField();
        jftxtEm = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbView = new javax.swing.JTable();
        jpnField = new javax.swing.JPanel();
        jchkNome = new javax.swing.JCheckBox();
        jchkTel = new javax.swing.JCheckBox();
        jchkEm = new javax.swing.JCheckBox();
        jchkNFunc = new javax.swing.JCheckBox();
        jbtView = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Visualização"));

        jpnParam.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros da busca"));

        jlbNome.setText("Nome");

        try {
            jftxtTel.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtTel.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        jlbTel.setText("Telefone");

        jlbEm.setText("Emissora (CNPJ)");

        jlbNFunc.setText("Nro. de Funcionários");

        try {
            jftxtEm.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtEm.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        javax.swing.GroupLayout jpnParamLayout = new javax.swing.GroupLayout(jpnParam);
        jpnParam.setLayout(jpnParamLayout);
        jpnParamLayout.setHorizontalGroup(
            jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnParamLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jftxtEm)
                    .addComponent(jlbNome, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jlbEm))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jlbNFunc)
                    .addComponent(jlbTel)
                    .addComponent(jftxtTel, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                    .addComponent(jtxtNFunc))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpnParamLayout.setVerticalGroup(
            jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnParamLayout.createSequentialGroup()
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpnParamLayout.createSequentialGroup()
                        .addComponent(jlbTel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jftxtTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpnParamLayout.createSequentialGroup()
                        .addComponent(jlbNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbEm)
                    .addComponent(jlbNFunc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNFunc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtEm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jtbView.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbView);

        jpnField.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a selecionar"));

        jchkNome.setText("Nome");

        jchkTel.setText("Telefone");

        jchkEm.setText("Emissora");

        jchkNFunc.setText("Nro. de Funcionários");

        javax.swing.GroupLayout jpnFieldLayout = new javax.swing.GroupLayout(jpnField);
        jpnField.setLayout(jpnFieldLayout);
        jpnFieldLayout.setHorizontalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkNome)
                    .addComponent(jchkTel)
                    .addComponent(jchkEm)
                    .addComponent(jchkNFunc))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        jpnFieldLayout.setVerticalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addComponent(jchkNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkTel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkEm)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkNFunc)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jbtView.setText("Visualizar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 715, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jpnParam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jpnField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jbtView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpnField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpnParam, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtView))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtView;
    private javax.swing.JCheckBox jchkEm;
    private javax.swing.JCheckBox jchkNFunc;
    private javax.swing.JCheckBox jchkNome;
    private javax.swing.JCheckBox jchkTel;
    private javax.swing.JFormattedTextField jftxtEm;
    javax.swing.JFormattedTextField jftxtTel;
    private javax.swing.JLabel jlbEm;
    private javax.swing.JLabel jlbNFunc;
    private javax.swing.JLabel jlbNome;
    private javax.swing.JLabel jlbTel;
    private javax.swing.JPanel jpnField;
    private javax.swing.JPanel jpnParam;
    private javax.swing.JTable jtbView;
    private javax.swing.JTextField jtxtNFunc;
    javax.swing.JTextField jtxtNome;
    // End of variables declaration//GEN-END:variables
    //ouvinte dos eventos do botão de visualização
    class DeptAction implements ActionListener {
        //ação do botão de visualização
        public void actionPerformed(ActionEvent e) {
            
            if (e.getSource() == jbtView) {
                
                ArrayList<String> param = new ArrayList<String>();
                ArrayList<String> field = new ArrayList<String>();
                
                if (jftxtEm.getText().compareTo("  .   .   /    -  ") != 0) {
                    param.add("UPPER(departamento.cnpjEm)=UPPER('"+jftxtEm.getText()+"')");
                }
                if (jftxtTel.getText().compareTo("          ") != 0) {
                    param.add("departamento.telefoneDe="+jftxtTel.getText());
                }
                if (jtxtNFunc.getText().compareTo("") != 0) {
                    param.add("departamento.nroFuncionariosDe="+jtxtNFunc.getText());
                }
                if (jtxtNome.getText().compareTo("") != 0) {
                    param.add("UPPER(departamento.nomeDe)=UPPER('"+jtxtNome.getText()+"')");
                }
                if (jchkEm.isSelected()) {
                    field.add("cnpjEm");
                }
                if (jchkTel.isSelected()) {
                    field.add("telefoneDe");
                }
                if (jchkNFunc.isSelected()) {
                    field.add("nroFuncionariosDe");
                }
                if (jchkNome.isSelected()) {
                    field.add("nomeDe");
                }

                String paramArray[] = new String[param.size()];
                String fieldArray[] = new String[field.size()];

                param.toArray(paramArray);
                field.toArray(fieldArray);
                
                try {
                    String[][] s = dbInterface.view(fieldArray, paramArray, new String[]{"departamento"});
                    if (fieldArray.length == 0) {
                        fieldArray = new String[5];
                        fieldArray[0] = "ID";
                        fieldArray[1] = "Nome";
                        fieldArray[2] = "Telefone";
                        fieldArray[3] = "Nro. de funcionarios";
                        fieldArray[4] = "CNPJ da emissora";
                    }
                    jtbView.setModel( new DefaultTableModel(s,fieldArray));
                } catch (SQLException ex) {
                    frame.printException(ex);
                    JOptionPane.showMessageDialog(null, "Erro ao tentar visualizar os dados.");
                }
                
            }
            
        }
        
    }

}
